/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.modules.admin.entity.EstateElevatorSafeUser;
import org.springblade.modules.admin.entity.EstateElevatorUser;
import org.springblade.modules.admin.service.EstateElevatorSafeUserService;
import org.springblade.modules.admin.vo.EstateElevatorUserVO;
import org.springblade.modules.admin.mapper.EstateElevatorUserMapper;
import org.springblade.modules.admin.service.IEstateElevatorUserService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.admin.vo.EstateSelectUserVO;
import org.springblade.modules.admin.vo.user.SafeUserVo;
import org.springblade.modules.system.vo.SimpleUserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 物业电梯管理人 服务实现类
 *
 * @author ysq
 * @since 2022-05-27
 */
@Service
public class EstateElevatorUserServiceImpl extends BaseServiceImpl<EstateElevatorUserMapper, EstateElevatorUser> implements IEstateElevatorUserService {

	@Autowired
	private EstateElevatorSafeUserService estateElevatorSafeUserService;

	@Override
	public IPage<EstateElevatorUserVO> selectEstateElevatorUserPage(IPage<EstateElevatorUserVO> page, EstateElevatorUserVO estateElevatorUser) {
		return page.setRecords(baseMapper.selectEstateElevatorUserPage(page, estateElevatorUser));
	}

	@Override
	public boolean removeElevatorUser(List<Long> elevatorIds) {
		return remove(Wrappers.<EstateElevatorUser>lambdaQuery().in(EstateElevatorUser::getElevatorId, elevatorIds));
	}

	@Override
	public boolean removeByUserIds(List<Long> userIds) {
		return remove(Wrappers.<EstateElevatorUser>lambdaQuery().in(EstateElevatorUser::getUserId, userIds));
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean distribution(EstateSelectUserVO selectUserVO) {
		List<EstateElevatorUser> savaList = new ArrayList<>();
		List<Long> elevatorIds = selectUserVO.getElevatorIds();
		// 删除已分配的人员
		this.removeElevatorUser(elevatorIds);
		for (Long elevatorId : elevatorIds) {
			for (SimpleUserVO userInfo : selectUserVO.getUserInfos()) {
				EstateElevatorUser estateElevatorUser = new EstateElevatorUser();
				estateElevatorUser.setElevatorId(elevatorId);
				estateElevatorUser.setUserId(userInfo.getUserId());
				estateElevatorUser.setUserName(userInfo.getUserName());
				savaList.add(estateElevatorUser);
			}
		}
		return this.saveBatch(savaList);
	}

	@Override
	public SafeUserVo checkElevatorUser(Long userId, String deptIds) {
		SafeUserVo safeUserVo = new SafeUserVo();
		Integer userCounts = baseMapper.selectCount(Wrappers.<EstateElevatorUser>lambdaQuery().eq(EstateElevatorUser::getUserId, userId));
		safeUserVo.setUserCounts(userCounts);
		int size = estateElevatorSafeUserService.list(Wrappers.<EstateElevatorSafeUser>lambdaQuery().eq(EstateElevatorSafeUser::getUserId, userId)).size();
		safeUserVo.setSafeUserCouts(size);
		if (StrUtil.isNotEmpty(deptIds)) {
			final List<String> deptIdss = Arrays.asList(deptIds.split(","));
			SafeUserVo safeUser = baseMapper.getElevatorUser(userId, deptIdss);
			safeUserVo.setDeptSafeUserCouts(Convert.toInt(safeUser.getDeptSafeUserCouts(), 0));
			safeUserVo.setDeptUserCounts(Convert.toInt(safeUser.getDeptUserCounts(), 0));
		}
		return safeUserVo;
	}

	@Override
	public List<Long> getEstateElevatorSafeUser(Long elevatorId) {
		return baseMapper.getEstateElevatorSafeUser(elevatorId);
	}

	@Override
	public boolean deleteElevatorUser(Long userId) {
		this.remove(Wrappers.<EstateElevatorUser>lambdaQuery().eq(EstateElevatorUser::getUserId, userId));
		estateElevatorSafeUserService.remove(Wrappers.<EstateElevatorSafeUser>lambdaQuery().in(EstateElevatorSafeUser::getUserId, userId));

		return true;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean deleteElevatorUser(Long userId, String deptIds) {
		List<String> deptIdss = Arrays.asList(deptIds.split(","));
		final List<Long> elevatorUserIds = baseMapper.getElevatorUserIds(userId, deptIdss);
		final List<Long> elevatorSafeUserIds = baseMapper.getElevatorSafeUserIds(userId, deptIdss);
		if (elevatorUserIds.size() > 0) {
			this.removeByIds(elevatorUserIds);
		}
		if (elevatorSafeUserIds.size() > 0) {
			estateElevatorSafeUserService.removeByIds(elevatorSafeUserIds);
		}
		return true;
	}
}
